<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Http\Services\TLSSigAPIv2;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends BaseController
{

    public function operate()
    {
        $user_id = auth('api')->id();

        // 发布猎聘数
        $recruitment_publish_count = DB::table('talents')->where('user_id',$user_id)->count('id');

        // 简历下载数
        $resume_download_count = DB::table('users')->where('id',$user_id)->value('resume_download_count');
        // 咨询数
        $consult_count = DB::table('users')->where('id',$user_id)->value('consult_count');

        // 广告接单数
        $ad_order_count = DB::table('ads')->where('order_user_id',$user_id)->count('id');
        // 圈子发布数
        $circle_publish_count = DB::table('circles')->where('user_id',$user_id)->count('id');


        return $this->success([
            'recruitment_publish_count' => $recruitment_publish_count,
            'resume_download_count' => $resume_download_count,
            'consult_count' => $consult_count,
            'ad_order_count' => $ad_order_count,
            'circle_publish_count' => $circle_publish_count,
        ]);
    }

    // 简历下载
    public function download()
    {
        $user_id = auth('api')->id();
        $result =   DB::table('users')->where('id',$user_id)->increment('resume_download_count');

        return $result ? $this->success() : $this->error();
    }


    // 咨询数
    public function consult()
    {
        $user_id = auth('api')->id();
        $result =   DB::table('users')->where('id',$user_id)->increment('consult_count');

        return $result ? $this->success() : $this->error();
    }


    public function show()
    {
        $user_id = auth('api')->id();
        $user = User::query()
                    ->where('id',$user_id)
                    ->with(['title:id,title','identity:user_id,identity'])
                    ->first();
        return $this->success($user);
    }

    public function sig()
    {
        $tlss = new TLSSigAPIv2('1600024408','937b0fee4af01338b7f5e608abdb6edd8736621a31f18fa057720667e923936c');

        $sig =  $tlss->genUserSig('uid'.auth('api')->id());


        return $this->success(['sig' => $sig]);
    }

    public function publish(Request $request)
    {

        $parma = $this->validate($request,[
            'user_id' => 'integer'
        ]);

        $user_id = $parma['user_id'] ??  auth('api')->id();
        $post_count = DB::table('articles')
            ->where('audit_status',2)
            ->where('type',1)
            ->where('user_id',$user_id)
            ->count('id');
        $article_count = DB::table('articles')
            ->where('audit_status',2)
            ->where('type',4)
            ->where('user_id',$user_id)
            ->count('id');
        $video_count = DB::table('articles')
            ->where('audit_status',2)
            ->where('type',2)
            ->where('user_id',$user_id)->count('id');
        $min_video_count = DB::table('articles')
            ->where('audit_status',2)
            ->where('type',3)
            ->where('user_id',$user_id)
            ->count('id');
        $topic_count = DB::table('article_categories')
            ->where('type',1)
            ->where('user_id',$user_id)
            ->count('id');
        $course_count = DB::table('courses')
            ->where('audit_status',2)
            ->where('user_id',$user_id)
            ->count('id');
        $ad_count = DB::table('ads')
            ->where('audit_status',2)
            ->where('user_id',$user_id)
            ->count('id');
        return $this->success([
            'post_count' => $post_count,
            'article_count' => $article_count,
            'video_count' => $video_count,
            'min_video_count' => $min_video_count,
            'topic_count' => $topic_count,
            'course_count' => $course_count,
            'ad_count' => $ad_count,
        ]);
    }


    public function center(Request $request)
    {
        $param = $this->validate($request,[
            'user_id' => 'integer'
        ]);

        $fuser_id = $param['user_id'] ?? 0;
        $user_id = auth('api')->id();
        $query = User::query();
        if ($fuser_id){
            $query->where('id',$fuser_id)
                ->selectRaw('*,(select count(friend_id) from user_follows where friend_id=users.id and user_id='.$user_id.' and status=1) as is_follow')
                ->withCount(['follow as f' => function($q) use ($fuser_id){
                    $q->where('user_id',$fuser_id)->where('status',1);
                }]);
        }else{
            $query->where('id',$user_id);
        }



        $user = $query->withCount('fans')
                    ->withCount(['follow' => function($q){
                       $q->where('status',1);
                    }])->withCount('alike')
                    ->withCount('like')
                    ->with(['title:id,title','identity','member' => function($q){
                        $q->with('member:id,name')->where('end_time','>',time());
                    }])
                    ->first();

        return $this->success($user);
    }

    public function update(Request $request)
    {
        $post = $this->validate($request,[
            'avatar' => 'max:255',
            'nickname' => 'max:60',
            'phone' => 'size:11',
            'birthday' => 'date_format:Y-m-d',
            'self_intro' => 'max:600',
            'wx_min_openid' => 'max:100',
            'wx_app_openid' => 'max:100'
        ],[
            'phone:size' => '手机号不合法'
        ]);


        $result = User::query()->where('id',auth('api')->id())->update($post);


        return $result ? $this->success() : $this->error();


    }
}
